package com.fishing.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fishing.entity.WoodsMap;
import com.fishing.util.personal.Guild_SortField;
import com.fishing.util.personal.SortField;

import QL.ButlerGuildBagItemsRequest;
import QL.ButlerGuildBagItemsResponse;
import QL.ButlerGuildBagLogRequest;
import QL.ButlerGuildBagLogResponse;
import QL.ButlerGuildDismissRequest;
import QL.ButlerGuildDismissResponse;
import QL.ButlerGuildGetListRequest;
import QL.ButlerGuildGetListResponse;
import QL.ButlerGuildSetDisplayLevelRequest;
import QL.ButlerGuildSetDisplayLevelResponse;
import QL.QLClient;
import net.sf.json.JSONArray;

/**
 * 公会管理 
 * @author lrz
 * */

@Controller
public class GuildController {
	private static QLClient client = new QLClient();
	
	@RequestMapping("guild/toList")
	public ModelAndView toList(HttpServletRequest request) {
		ModelAndView mav = new  ModelAndView("guild/list");
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="guild/List",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public  Map List(HttpServletRequest request,String PageIndex,String PageCount,String FilterField,String FilterType,String DisplayType) {
		Map<String,Object> map = new HashMap<String, Object>();
		ButlerGuildGetListRequest req = new ButlerGuildGetListRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		if(FilterField == null && FilterType == null) {
			req.FilterField = 1;
			req.FilterType = 1;
		}else {
			for(Guild_SortField field : Guild_SortField.values()) {
    			if(field.name().equals(FilterField)) {
    				req.FilterField = field.getValue();
    				break;
    			}
    		}
			req.FilterType = FilterType.equals("desc")?1:2;
		}
		req.PageIndex = Integer.parseInt(PageIndex) - 1;
		req.PageCount = Integer.parseInt(PageCount);
		req.DisplayType = Integer.parseInt(DisplayType);
		ButlerGuildGetListResponse rsp = new ButlerGuildGetListResponse();
		try {
			if (req.ZoneId < 0)
				client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
			client.Execute(req, rsp);
		} catch (Exception e) {
		}
		map.put("data", rsp.GuildList);
		map.put("count", rsp.TotalCount);
		map.put("status", "200");
		map.put("message", "");
		return map;
	}
	
	
	@RequestMapping("guild/openwarehouse")
	public ModelAndView openguild(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("guild/openwarehouse");
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="guild/WarehouseList",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
	public Map WarehouseList(HttpServletRequest request,String guildId) {
		Map<String,String> mapdata = new HashMap<>();
		for(Map.Entry<String, String> entry : WoodsMap.woodsmap().entrySet()) {
        	//初始化一个默认值
			mapdata.put(entry.getValue(), "\\");
		}
		ButlerGuildBagItemsRequest req = new ButlerGuildBagItemsRequest();
        try {
        	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    		req.GuildId = Long.parseLong(guildId);
    		ButlerGuildBagItemsResponse rsp = new ButlerGuildBagItemsResponse();
    		if (req.ZoneId < 0)
    			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
    		client.Execute(req, rsp);
        	
        	//把String转为String数组
			String[] strs = rsp.ItemString.split("\\|");
			//当用entrySet()方法遍历 时  需要注意map的返回值 ，要加上 泛型
			for(int i = 0;i < strs.length;i++) {
				for(Map.Entry<String, String> entry : WoodsMap.woodsmap().entrySet()) {
		        	//判断当前返回数据的字符串是哪一个物品,把物品名称变成字段名返回map
					if(strs[i].split(",")[0].equals(entry.getKey())) {
						mapdata.put(entry.getValue(), strs[i].split(",")[1]);
					}
		        }
			}
			Map<String,Object> map = new HashMap<>();
	        map.put("data", JSONArray.fromObject(mapdata));
	        map.put("count", mapdata.size());
	        map.put("status", "200");
	        map.put("message", "");
	        return map;
		} catch (Exception e) {
			e.printStackTrace();
			Map<String,Object> map = new HashMap<>();
	        map.put("data", 0);
	        map.put("count", 0);
	        map.put("status", "200");
	        map.put("message", "");
	        return map;
		}
	}
	
	@ResponseBody
	@RequestMapping(value="guild/dissolution",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public String dissolution(HttpServletRequest request, String ID) {
		ButlerGuildDismissRequest req = new ButlerGuildDismissRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		req.GuildId = Integer.parseInt(ID);
		ButlerGuildDismissResponse rsp = new ButlerGuildDismissResponse();
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		return rsp.ErrMsg;
	}
	
	@RequestMapping("guild/openlistjournal")
	public ModelAndView tolistjournal(HttpServletRequest request) {
		ModelAndView mav = new  ModelAndView("guild/openlistjournal");
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "guild/JournalList",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public Map JournalList(HttpServletRequest request,String guildId,String PageIndex,String PageCount) {
		ButlerGuildBagLogRequest req = new ButlerGuildBagLogRequest();
		Map<String,Object> map = new HashMap<>();
		try {
			req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
			req.PageIndex = Long.parseLong(PageIndex) - 1;
			req.PageCount = Long.parseLong(PageCount);
			req.GuildId = Long.parseLong(guildId);
			ButlerGuildBagLogResponse rsp = new ButlerGuildBagLogResponse();
			if (req.ZoneId < 0)
				client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
			client.Execute(req, rsp);
			map.put("status", "200");
			map.put("message", "");
			map.put("data", rsp.ItemLogList);
			map.put("count", rsp.TotalCount);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "200");
			map.put("message", "");
			map.put("data", 0);
			map.put("count", 0);
			return map;
		}
	}
	
	@RequestMapping("guild/chanegstate")
	public ModelAndView changestate(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("guild/changestate");
		return  mav;
	}
	
	@ResponseBody
	@RequestMapping(value="guild/SetState",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public String SetState(HttpServletRequest request,String GuildId,String DisplayLevel) {
		ButlerGuildSetDisplayLevelRequest req = new ButlerGuildSetDisplayLevelRequest();
		try {
			req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
			req.GuildId = Integer.parseInt(GuildId);
			req.DisplayLevel = Integer.parseInt(DisplayLevel);
			ButlerGuildSetDisplayLevelResponse rsp = new ButlerGuildSetDisplayLevelResponse();
			if (req.ZoneId < 0)
				client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
			client.Execute(req, rsp);
			return rsp.ErrMsg;
		} catch (Exception e) {
			e.printStackTrace();
			return "设置失败";
		}
	}
}
